/**
 * Copyright 2009-2011 xiaonei.com All right reserved.
 * This software is the confidential and proprietary information of Xiaonei.com ("Confidential Information"). 
 * You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with Xiaonei.com.
 * Create by XiaoNeiAntiSpamTeam
 *
 */

package com.xiaonei.testice.testicebox.client;

import Ice.Communicator;
import Ice.NoEndpointException;
import Ice.ObjectPrx;
import IceGrid.AllocationException;
import IceGrid.ObjectNotRegisteredException;
import IceGrid.PermissionDeniedException;
import IceGrid.RegistryPrx;
import IceGrid.RegistryPrxHelper;
import IceGrid.SessionPrx;

import com.xiaonei.testice.testicebox.PrinterPrx;
import com.xiaonei.testice.testicebox.PrinterPrxHelper;
import com.xiaonei.testice.util.IceUtil;

/**
 * @ClassName: PrinterClient
 * @Description: Printer客户端
 * @author shaohui.liu shaohui.liu@opi-corp.com
 * @date Feb 20, 2010 10:51:54 AM
 * 
 */
public class PrinterSessionClient {

    private PrinterPrx printerPrx = null;

    public static PrinterSessionClient instance = new PrinterSessionClient();

    public static PrinterSessionClient getInstance() {
        return instance;
    }

    private PrinterSessionClient() {
        try {
            Communicator co = Ice.Util.initialize(IceUtil.initData());

            RegistryPrx registryPrx = RegistryPrxHelper.checkedCast(co
                    .stringToProxy("XiaoNeiAntiSpam/Registry"));
            SessionPrx sessionPrx = null;

            try {
                sessionPrx = registryPrx.createSession("safe", "safe");
                sessionPrx = (SessionPrx) sessionPrx.ice_timeout(6000);
                sessionPrx.keepAlive();

                System.out.println(sessionPrx);
            } catch (PermissionDeniedException e) {
                e.printStackTrace();
            }
            ObjectPrx objectPrx = null;
            objectPrx = sessionPrx.allocateObjectById(co.stringToIdentity("Printer"));
            printerPrx = PrinterPrxHelper.uncheckedCast(objectPrx);
            
            System.out.println(printerPrx);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Title: print
     * @Description: 打印消息
     * @param @param message
     * @return void
     * @throws
     */
    public void print(String message) {
        try {
            printerPrx.printString(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        PrinterSessionClient printerClient = PrinterSessionClient.getInstance();

        for (int i = 0; i < 2; i++) {
            long start = System.currentTimeMillis();
            printerClient.print("HI!");
            System.out.println(System.currentTimeMillis() - start);
        }
        System.exit(0);
    }
}
